define(['jquery','cookie'],function($){
	var soKey='SOSESSID',
		wxKey = 'SOWXOPENID',
		usernameKey='username',
		nicknameKey='nickname',
		wxavatarKey='wxavatar',
		accountKey='account',
		mobileKey='mobile',
		uidKey='uid',
		pidKey='parentid',
	    isRegister=$.cookie('isRegister'),
	    cookieUserKey='user',
	    cookieUser={},//存储在cookie的所有用户数据
	    hasLocalStorage = window.localStorage ? true : false;
	return {
		checkLogin:function(){
			if($.cookie(soKey)){
				return true;
			}else{
			    window.location.hash="#/login";
			    return false;
			}
		},
		isLogin:function(){
			return !!$.cookie(soKey);
		},
		loginOut:function(){
			$.removeCookie(soKey);
			this.clearCookieUser()
			this.clearAgent();

			if(hasLocalStorage){
				localStorage.removeItem(usernameKey);
				localStorage.removeItem(accountKey);
				localStorage.removeItem(uidKey);
				localStorage.removeItem(pidKey);
				localStorage.removeItem(nicknameKey);
				localStorage.removeItem(wxavatarKey);
			}
			location.hash=location.hash=="#/"||location.hash==''?'#/login':"#/";
		},
		setIsRegister:function(b){
			isRegister=b;
		},
		isRegister:function(){
			return isRegister=='1';
		},
		key:function(){
			return soKey;
		},
		setCookieUser:function(key,val){ //存储用户数据(所有数据都存在一条记录)
			var temp = {},
				old = $.cookie(cookieUserKey) ? JSON.parse($.cookie(cookieUserKey)) : {};
				
			if(old){
				for (k in old) {
					temp[k]=old[k];
				}
			}
			temp[key]=val;
			$.cookie(cookieUserKey, JSON.stringify(temp));
		},
		getCookieUser:function(key){
			if(key){
				return $.cookie(cookieUserKey) ? JSON.parse($.cookie(cookieUserKey))[key] : null;
			}else{
				return $.cookie(cookieUserKey) ? JSON.parse($.cookie(cookieUserKey)) : null;
			}
		},
		clearCookieUser:function(){
			$.removeCookie(cookieUserKey);
		},
		getSessionObject:function(){
			return {'SOSESSID':$.cookie(soKey)};
		},
		setAppid:function(val){ //是否订阅公众号
			hasLocalStorage ? localStorage.setItem('appid', val) : this.setCookieUser('appid',val);
		},
		getAppid:function(){
			return hasLocalStorage ? localStorage.getItem('appid') : this.getCookieUser('appid');
		},
		setSubscribe:function(val){ //是否订阅公众号
			hasLocalStorage ? localStorage.setItem('subscribe', val) : this.setCookieUser('subscribe',val);
		},
		getSubscribe:function(){
			return hasLocalStorage ? localStorage.getItem('subscribe') : this.getCookieUser('subscribe');
		},
		setHeadimgurl:function(val){
			hasLocalStorage ? localStorage.setItem('headimgurl', val) : this.setCookieUser('headimgurl',val);
		},
		getHeadimgurl:function(){
			return hasLocalStorage ? localStorage.getItem('headimgurl') : this.getCookieUser('headimgurl');
		},
		setNickname:function(val){
			hasLocalStorage ? localStorage.setItem(nicknameKey, val) : this.setCookieUser(nicknameKey,val);
		},
		getNickname:function(){
			return hasLocalStorage ? localStorage.getItem(nicknameKey) : this.getCookieUser(nicknameKey);
		},
		setUsername:function(val){
			hasLocalStorage ? localStorage.setItem(usernameKey, val) : this.setCookieUser(usernameKey,val);
		},
		getUsername:function(){
			return hasLocalStorage ? localStorage.getItem(usernameKey) : this.getCookieUser(usernameKey);
		},
		setUseraccount:function(val){
			hasLocalStorage ? localStorage.setItem(accountKey, val) : this.setCookieUser(accountKey,val); 
		},
		getUseraccount:function(){
			return hasLocalStorage ? localStorage.getItem(accountKey) : this.getCookieUser(accountKey);
		},
		setMobile:function(val){
			hasLocalStorage ? localStorage.setItem(mobileKey, val) : this.setCookieUser(mobileKey,val);
		},
		getMobile:function(){
			return hasLocalStorage ? localStorage.getItem(mobileKey) : this.getCookieUser(mobileKey);
		},
		setUid:function(val){
			hasLocalStorage ? localStorage.setItem(uidKey, val) : this.setCookieUser(uidKey,val);
		},
		getUid:function(){
			return hasLocalStorage ? localStorage.getItem(uidKey) : this.getCookieUser(uidKey);
		},
		setPid:function(val){
			hasLocalStorage ? localStorage.setItem(pidKey, val) : this.setCookieUser(pidKey,val);
		},
		getPid:function(){
			return hasLocalStorage ? localStorage.getItem(pidKey) : this.getCookieUser(pidKey);
		},
		setWeixinid:function(val){
			$.cookie(wxKey,val,{expires:7});
		},
		getWeixinid:function(){
			return $.cookie(wxKey);
		},
		setWeixinAvatar:function(val){
			hasLocalStorage ? localStorage.setItem(wxavatarKey, val) : this.setCookieUser(wxavatarKey,val);
		},
		getWeixinAvatar:function(){
			if(hasLocalStorage){
				return localStorage.getItem(wxavatarKey) ? localStorage.getItem(wxavatarKey) : '/static/img/avatar.jpg';
			}else{
				return this.getCookieUser(wxavatarKey) ? this.getCookieUser(wxavatarKey) : '/static/img/avatar.jpg';
			}
		},
		set:function(key,val){
			hasLocalStorage ? localStorage.setItem(key, val) : $.cookie(key,val);
		},
		get:function(key){
			return hasLocalStorage ? localStorage.getItem(key) : $.cookie(key);
		},
		getData:function(){
			return {
				username:this.getUsername(),
				useraccount:this.getUseraccount(),
				mobile:this.getMobile(),
				uid:this.getUid(),
				pid:this.getPid(),
				headimgurl:this.getWeixinAvatar()
			};
		},
		addAgent:function(uid,pid){//推荐人信息
			if(hasLocalStorage){
				localStorage.setItem('agentid', uid);
				localStorage.setItem('agentpid', pid);
			}else{
	       		$.cookie('agentid',uid);
				$.cookie('agentpid',pid);
			}
		},
		getAgent:function(){
			var uid,pid;
			if(hasLocalStorage){
				uid = localStorage.getItem('agentid');
				pid = localStorage.getItem('agentpid');
			}else{
				uid = $.cookie('agentid');
				pid = $.cookie('agentpid');
			}
			return {agentid:uid,agentpid:pid};
		},
		clearAgent:function(){
			if(hasLocalStorage){
				localStorage.removeItem('agentpid');
				localStorage.removeItem('agentid');
			}else{
				$.removeCookie('agentid');
				$.removeCookie('agentpid');
			}
		}
	}
})